define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'hotel/order/index' + location.search,
                    add_url: 'hotel/order/add',
                    //edit_url: 'hotel/order/edit',
                    del_url: 'hotel/order/del',
                    multi_url: 'hotel/order/multi',
                    import_url: 'hotel/order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'order_id',
                sortName: 'order_id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'order_id', title: __('Order_id')},

                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},

                        {field: 'room.room_name', title: __('Room.room_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'room.image', title: __('Room.image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'checkin_date', title: __('Checkin_date'), operate: 'LIKE'},
                        {field: 'leave_date', title: __('Leave_date'), operate: 'LIKE'},
                        {field: 'realname', title: __('Realname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'day', title: __('Day')},
                        {field: 'pay_type', title: __('Pay_type'), formatter: Table.api.formatter.status, searchList: {'': __('待支付'),'offlinepay': __('线下支付'), 'wechat': __('微信支付'), 'balance': __('余额支付')}},
                        {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order_status', title: __('Order_status'), formatter: Table.api.formatter.status, searchList: {'-1': __('已取消'), 0: __('待支付'), 1: __('待入住'), 4: __('售后'), 10: __('已完成')}},
                        {field: 'order_money', title: __('Order_money'), operate:'BETWEEN'},
                        {field: 'pay_money', title: __('Pay_money'), operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {
                            field: 'operate', title: __('Operate'),
                            table: table, events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name: 'detail',
                                    text: __('详情'),
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'hotel/order/detail',
                                },
                                {
                                    name: 'checkin',
                                    text: __('确认入住'),
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'hotel/order/checkin',
                                    visible:function(row){
                                        if(row.order_status == 1){
                                            return true;
                                        }
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'hotel/order/recyclebin' + location.search,
                pk: 'order_id',
                sortName: 'order_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'order_id', title: __('订单id')},
                        {field: 'order_no', title: __('订单编号')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'hotel/order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'hotel/order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        checkin: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
